﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ButtonTree2
{
    public partial class ButtonBase : UserControl
    {
        private RibbonMenuStrategy _menuStrategy = new RibbonMenuStrategy();
        public ButtonBase()
        {
            InitializeComponent();
            InitOther();
           
        }
        public ButtonBase(ContextMenuStrip Menu,Boolean is_Clickable)
        {
            InitializeComponent();
            InitOther();
            _menuStrategy.Menu = Menu;
            _menuStrategy.Menu.Renderer = MenuItemRanderer.Instance;
            _lineTextMargin = new Padding(22, 3, 14, 3);
            this.is_Clickable = is_Clickable;
        }
        public void InitOther () 
        {
            if (_isLarge)
            {
                _lineText = new TwoLineText();
            }
            else
            {
                _lineText = new OneLineText();
            }
            InitMouseEventStrategy();


            _menuStrategy.MenuClosedEventHandler = new EventHandler<ToolStripDropDownClosedEventArgs>(Menu_Closed);
        }

        // 菜单关闭后需要刷新界面
        private void Menu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            Invalidate();
        }

        #region 属性
        public Boolean is_Clickable = true;

        protected bool IsMouseDown
        {
            get
            {
                if (_mouseEventStrategy != null)
                {
                    return _mouseEventStrategy.IsMouseDown;
                }
                else
                {
                    return false;
                }
            }
        }
        private Brush _lineTextBrush = Brushes.Black;
        private bool _isLarge = false;
        protected bool IsMenuShowing
        {
            get
            {
                return _menuStrategy.Menu.Visible;
                //return _menuStrategy.IsMenuShowing;
            }
        }
        #endregion


        #region 鼠标事件策略

        private MouseEventStrategy _mouseEventStrategy = new MouseEventStrategy();

        private void InitMouseEventStrategy()
        {
            _mouseEventStrategy.AddControlMouseEvent(this);
            _mouseEventStrategy.MouseHoverStateChangeAction = new Action<bool>(OnMouseHoverStateChanged);
            _mouseEventStrategy.ClickEventHandler = new EventHandler(OnControlClick);
            _mouseEventStrategy.MouseLeftButtonDown = new EventHandler(OnControlMouseDown);
        }

        private void OnControlMouseDown(object sender, EventArgs args)
        {
            Invalidate(this.ClientRectangle);
        }

        private void OnMouseHoverStateChanged(bool bMouseHover)
        {
            Invalidate(this.ClientRectangle);
            
        }

        private void OnControlClick(object sender, EventArgs e)
        {

            if (is_Clickable == true)
            {

                MessageBox.Show("Click...");
            }
            if (this._menuStrategy.Menu != null)
            {
                ShowMenu();
            }
        }
        public void ShowMenu ()
        {

            _menuStrategy.ShowMenu(this);
            Invalidate(true);
        }
        #endregion

        #region LineText相关

        private ILineText _lineText;

        private Padding _lineTextMargin;   // 文字距离周围边框的距离，子类型控件应该在构造器中修改该值

        public string LineText
        {
            get
            {
                return _lineText.Text;
            }
            set
            {
                _lineText.Text = value;
                using (Graphics g = this.CreateGraphics())
                {
                    _lineText.UpdateTextSize(g, RibbonConfigUtils.TextNormalFont);
                }

            }
        }

        protected void DrawLineText(Graphics g, Font font)
        {
            //_lineText.Draw(g, font, brush, LineTextArea);

            Point locLineText;
            Size sizeLineText = _lineText.GetHintSize();

            if (_isLarge)
            {
                locLineText = AlignUtils.GetAlignedPosition(ClientRectangle, sizeLineText.Width, sizeLineText.Height, ContentAlignment.TopCenter, _lineTextMargin);
            }
            else
            {
                locLineText = AlignUtils.GetAlignedPosition(ClientRectangle, sizeLineText.Width, sizeLineText.Height, ContentAlignment.MiddleRight, _lineTextMargin);
            }
            Rectangle lineTextArea = new Rectangle(locLineText, sizeLineText);
            Brush brush = GetTextBrush();
            _lineText.Draw(g, font, brush, lineTextArea);
        }

        #endregion

        protected bool IsMouseHover
        {
            get
            {
                if (_mouseEventStrategy != null)
                {
                    return _mouseEventStrategy.IsMouseHover;
                }
                else
                {
                    return false;
                }
            }
        }
        protected virtual Brush GetTextBrush()
        {
            return this.Enabled ? _lineTextBrush : Brushes.Gray;
        }

        private void Button_Paint (object sender,PaintEventArgs e) 
        {


            if (IsMouseDown || IsMenuShowing)
            {
                RibbonPaintUtils.PaintSmallWhenMouseDown(e.Graphics, this.ClientRectangle, 0, false);
            }
            else if (IsMouseHover)
            {
                RibbonPaintUtils.PaintSmallWhenMouseHover(e.Graphics, this.ClientRectangle, 0, false);
            }
            DrawLineText(e.Graphics, RibbonConfigUtils.TextNormalFont);
            if (_menuStrategy.Menu != null)
            {
                RibbonPaintUtils.PaintSmallTriangle(e.Graphics, this.ClientRectangle, HorizontalAlignment.Right,
                System.Windows.Forms.VisualStyles.VerticalAlignment.Center, 5, new Padding(5, 5, 5, 5), Color.Black);
            }
        }
    }



}
